package com.example.teamtribe.main.club.manage.feedback.apply;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.teamtribe.R;
import com.example.teamtribe.base.BaseActivity;
import com.example.teamtribe.database.TeamRequest;
import com.example.teamtribe.utils.Utility;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class AppListActivity extends BaseActivity implements AppListContract.View, AppListAdapter.Agree {

    private XRecyclerView rvAppList;
    private AppListAdapter adapter;
    private List<TeamRequest> requests;
    private AppListContract.Presenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app_list);
        ((TextView) findViewById(R.id.tv_title)).setText("申请");
        final int teamId = getIntent().getIntExtra("teamId", 0);
        presenter = new AppListPresenter(this);
        rvAppList = findViewById(R.id.rv_app_list);
        rvAppList.setNestedScrollingEnabled(false);
        rvAppList.setLayoutManager(new LinearLayoutManager(this));
        rvAppList.setRefreshHeader(rvAppList.getDefaultRefreshHeaderView());
        requests = new ArrayList<>();
        adapter = new AppListAdapter(requests, this);
        rvAppList.setAdapter(adapter);
        rvAppList.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                presenter.start(teamId);
            }

            @Override
            public void onLoadMore() {
                presenter.getRequest();
            }
        });
        presenter.start(teamId);
        ImageView ivShare = findViewById(R.id.iv_more);
        ivShare.setVisibility(View.VISIBLE);
        ivShare.setImageResource(R.drawable.ic_share);
        ivShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utility.makeToast("已导出至/sdcard/TeamTribe/招新表.xlsx", Toast.LENGTH_SHORT);
            }
        });
    }

    @Override
    public void addRequest(TeamRequest request) {
        requests.add(request);
        Collections.sort(requests);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void addRequest(List<TeamRequest> requests) {
        this.requests.addAll(requests);
        Collections.sort(this.requests);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void clearRequest() {
        requests.clear();
    }

    @Override
    public void notifyItemChanged(final int position, TeamRequest request) {
        if (request != null) {
            requests.set(position, request);
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyItemChanged(position + 1);//header占了第一个位置，所以data从1算起
            }
        });
    }

    @Override
    public void loadComplete() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                rvAppList.refreshComplete();
                rvAppList.loadMoreComplete();
            }
        });
    }

    @Override
    public void agree(int position) {
        presenter.agree(position, requests.get(position));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == AppListAdapter.REQUEST && resultCode == RESULT_OK) {
            TeamRequest request = data.getParcelableExtra("request");
            int index = requests.indexOf(request);
            if (index >= 0) {
                notifyItemChanged(index, request);
            }
        }
    }
}
